#include "protocol256.h"
// CRC8查找表，基于多项式 0x31 (x^8 + x^5 + x^4 + 1)
static const uint8_t crc8_table[256] = {
    0x00, 0x31, 0x62, 0x53, 0xC4, 0xF5, 0xA6, 0x97, 0xB9, 0x88, 0xDB, 0xEA, 0x7D, 0x4C, 0x1F, 0x2E,
    0x43, 0x72, 0x21, 0x10, 0x87, 0xB6, 0xE5, 0xD4, 0xFA, 0xCB, 0x98, 0xA9, 0x3E, 0x0F, 0x5C, 0x6D,
    0x86, 0xB7, 0xE4, 0xD5, 0x42, 0x73, 0x20, 0x11, 0x3F, 0x0E, 0x5D, 0x6C, 0xFB, 0xCA, 0x99, 0xA8,
    0xC5, 0xF4, 0xA7, 0x96, 0x01, 0x30, 0x63, 0x52, 0x7C, 0x4D, 0x1E, 0x2F, 0xB8, 0x89, 0xDA, 0xEB,
    0x3D, 0x0C, 0x5F, 0x6E, 0xF9, 0xC8, 0x9B, 0xAA, 0x84, 0xB5, 0xE6, 0xD7, 0x40, 0x71, 0x22, 0x13,
    0x7E, 0x4F, 0x1C, 0x2D, 0xBE, 0x8F, 0xD8, 0xE9, 0xC7, 0xF6, 0xA5, 0x94, 0x03, 0x32, 0x61, 0x50,
    0xBB, 0x8A, 0xD9, 0xE8, 0x7F, 0x4E, 0x1D, 0x2C, 0x02, 0x33, 0x60, 0x51, 0xC6, 0xF7, 0xA4, 0x95,
    0xF8, 0xC9, 0x9A, 0xAB, 0x3C, 0x0D, 0x5E, 0x6F, 0x41, 0x70, 0x23, 0x12, 0x85, 0xB4, 0xE7, 0xD6,
    0x7A, 0x4B, 0x18, 0x29, 0xBE, 0x8F, 0xDC, 0xED, 0xC3, 0xF2, 0xA1, 0x90, 0x07, 0x36, 0x65, 0x54,
    0x39, 0x08, 0x5B, 0x6A, 0xFD, 0xCC, 0x9F, 0xAE, 0x80, 0xB1, 0xE2, 0xD3, 0x44, 0x75, 0x26, 0x17,
    0xFC, 0xCD, 0x9E, 0xAF, 0x38, 0x09, 0x5A, 0x6B, 0x45, 0x74, 0x27, 0x16, 0x81, 0xB0, 0xE3, 0xD2,
    0xBF, 0x8E, 0xDD, 0xEC, 0x7B, 0x4A, 0x19, 0x28, 0x06, 0x37, 0x64, 0x55, 0xC2, 0xF3, 0xA0, 0x91,
    0x47, 0x76, 0x25, 0x14, 0x83, 0xB2, 0xE1, 0xD0, 0xFE, 0xCF, 0x9C, 0xAD, 0x3A, 0x0B, 0x58, 0x69,
    0x04, 0x35, 0x66, 0x57
};
// CRC 高位字节值表
static const uint8_t s_CRCHi[] = {
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
} ;
// CRC 低位字节值表
static const uint8_t s_CRCLo[] = {
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0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x41, 0x81, 0x80, 0x40
};
// CRC8更新函数
static uint8_t crc8_update(uint8_t crc, uint8_t data) {
    return crc8_table[crc ^ data];
}
static uint16_t CRC16_Modbus(uint8_t *_pBuf, uint16_t _usLen)
{
    uint8_t ucCRCHi = 0xFF; /* 高CRC字节初始化 */
    uint8_t ucCRCLo = 0xFF; /* 低CRC 字节初始化 */
    uint16_t usIndex;  /* CRC循环中的索引 */

    while (_usLen--)
    {
        usIndex = ucCRCHi ^ *_pBuf++; /* 计算CRC */
        ucCRCHi = ucCRCLo ^ s_CRCHi[usIndex];
        ucCRCLo = s_CRCLo[usIndex];
    }
    return ((uint16_t)ucCRCHi << 8 | ucCRCLo);
}
// 计算整个数据缓冲区的CRC8校验
uint8_t calculate_crc8(const uint8_t *data, uint8_t length) {
    uint8_t crc = 0x00;

   crc = CRC16_Modbus((uint8_t *)data, length);

    return crc;
}

// 协议解析器初始化
void protocol256_parser_init(protocol_parser_t *parser, data_callback_t callback, void *param) {
    parser->state = STATE_IDLE;
    parser->buffer_index = 0;
    parser->length = 0;
    parser->callback = callback;
    parser->param = param;
}

// 协议解析函数
void protocol_parse_byte(protocol_parser_t *parser, uint8_t byte) {
    uint8_t expected_crc;
    switch (parser->state) {
        case STATE_IDLE:
            if (byte == 0xAA) {  // 帧头第一个字节
                parser->buffer[0] = byte;
                parser->buffer_index = 1;
                parser->state = STATE_HEADER_1;
            }
            break;

        case STATE_HEADER_1:
            if (byte == 0x55) {  // 帧头第二个字节
                parser->buffer[1] = byte;
                parser->buffer_index = 2;
                parser->state = STATE_HEADER_2;
            } else {
                parser->state = STATE_IDLE;  // 返回初始状态
            }
            break;

        case STATE_HEADER_2:
            parser->buffer[parser->buffer_index++] = byte;  // 功能码
            parser->state = STATE_FUNC_CODE;
            break;

        case STATE_FUNC_CODE:
            parser->buffer[parser->buffer_index++] = byte;  // 长度
            parser->length = byte;

            if (parser->length > 0) {
                parser->state = STATE_PAYLOAD;
            } else {
                parser->state = STATE_CRC;
            }
            break;

        case STATE_PAYLOAD:
            parser->buffer[parser->buffer_index++] = byte;  // 数据域
            if (parser->buffer_index == parser->length + 4) {  // 数据域读取完成
                parser->state = STATE_CRC;
            }
            break;

        case STATE_CRC:
            expected_crc = calculate_crc8(parser->buffer, parser->buffer_index);
            if (expected_crc == byte) {
                // CRC校验通过，调用回调函数
                parser->callback(parser->buffer[2], parser->buffer + 4, parser->length,parser->param);
            }
            parser->state = STATE_IDLE;  // 重置解析状态
            break;

        default:
            parser->state = STATE_IDLE;  // 错误时重置
            break;
    }
}

// 协议封装函数
uint8_t protocol_pack(uint8_t func_code, const uint8_t *payload, uint8_t payload_len, uint8_t *out_buffer) {


    out_buffer[0] = 0xAA;  // 帧头
    out_buffer[1] = 0x55;
    out_buffer[2] = func_code;     // 功能码
    out_buffer[3] = payload_len;  // 数据域长度

    // 复制数据域
    for (uint8_t i = 0; i < payload_len; i++) {
        out_buffer[4 + i] = payload[i];
    }

    // 计算CRC8校验
    uint8_t crc = calculate_crc8(out_buffer, 4 + payload_len);
    out_buffer[4 + payload_len] = crc;

    return 5 + payload_len; // 返回封装后数据包的长度
}
int protocol_parse_packet(protocol_parser_t *parser,  uint8_t *packet, uint8_t length) {
    // 校验包头长度
    if (length < 5) {
        return -1;  // 数据包太短，无法解析
    }

    // 校验包头
    if (packet[0] != 0xAA || packet[1] != 0x55) {
        return -2;  // 包头不匹配
    }

    // 获取功能码和数据长度
    uint8_t func_code = packet[2];
    uint8_t data_len = packet[3];

    // 检查数据长度是否匹配
    if (data_len + 5 != length) {
        return -3;  // 数据包长度不匹配
    }

    // 计算CRC8校验
    uint8_t expected_crc = calculate_crc8(packet, length - 1); // 不计算CRC位
    uint8_t received_crc = packet[length - 1];

    // 校验CRC8
    if (expected_crc != received_crc) {
        return -4;  // CRC校验失败
    }

    // 校验通过，调用回调函数
    parser->callback(func_code, packet + 4, data_len, parser->param);

    return 0;  // 返回0表示解析成功
}
